﻿
using DongleTest;
using DongleTest.DB;
using IntegrationTest.Properties;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Test;

namespace IntegrationTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Load += MainForm_Load;
            this.Shown += MainForm_Shown;
            this.FormClosing += MainForm_FormClosing;
            this.Text = IniReaderAndWriter.GetIniValue("Setting", "AppName", "成品测试");
            this.label1.Text = IniReaderAndWriter.GetIniValue("Setting", "Tips", "成品测试");
        }

        private List<string> ports = new List<string>();

        private ArrayList tests = new ArrayList();


        /// <summary>
        /// 界面加载初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now;

            foreach (string port in PortsInfo.SerialPort())
            {
                if (port.ToUpper().Contains("CC2540"))
                {
                    ports.Add(port.ToUpper());
                }
            }
            string chip = IniReaderAndWriter.GetIniValue("Setting", "Chip", "NORDIC");

            string mode = IniReaderAndWriter.GetIniValue("Setting", "Mode", "A");

            string function = IniReaderAndWriter.GetIniValue("Setting", "Function", "T");

            Upload.SetUrlType(mode, function);

            int count = ports.Count;
            for (int i = 0; i < count; i++)
            {
                SingleTest st = new SingleTest();
                st.PortName = ports[i];
                st.Init();
                st.Chip = chip;

                st.Size = new Size(panelMain.Width / count, panelMain.Height);
                st.Location = new Point(i * (panelMain.Width / count), 0);
                this.panelMain.Controls.Add(st);
                st.Index = i;
                st.EnterKeyDown += st_EnterKeyDown;
                st.EndTestEvent += st_EndTestEvent;
                st.Selected += st_Selected;
                tests.Add(st);
            }


        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            for (int i = 0; i < tests.Count; i++)
            {
                SingleTest item = (SingleTest)tests[i];

                if (i == 0)
                {
                    item.Select(true);
                }
                else
                {
                    item.Select(false);
                }
            }
            if (!Upload.IsConnectInternet)
            {
                MessageBox.Show("网络连接不可用！");

                for (int i = 0; i < tests.Count; i++)
                {
                    SingleTest item = (SingleTest)tests[i];
                    item.Enabled = false;
                }
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            for (int i = 0; i < tests.Count; i++)
            {
                SingleTest item = (SingleTest)tests[i];
                item.End();
            }
        }

        private void st_Selected(int index)
        {
            for (int i = 0; i < tests.Count; i++)
            {
                SingleTest item = (SingleTest)tests[i];
                if (index % tests.Count == i)
                {
                    item.Select(true);
                }
                else
                {
                    item.Select(false);
                }
            }
        }

        private void st_EnterKeyDown(int index)
        {

            for (int i = 0; i < tests.Count; i++)
            {
                SingleTest item = (SingleTest)tests[i];
                if ((index + 1) % tests.Count == i)
                {
                    item.Select(true);
                }
                else
                {
                    item.Select(false);
                }
            }

        }

        private void st_EndTestEvent()
        {
            dateTimePicker1.Invoke(new EventHandler(delegate
            {
                dateTimePicker1.Value = DateTime.Now;
            }));
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            int count = tests.Count;
            for (int i = 0; i < count; i++)
            {
                SingleTest item = (SingleTest)tests[i];
                item.Size = new Size(panelMain.Width / count, panelMain.Height);
                item.Location = new Point(i * (panelMain.Width / count) + 1, 0);
            }
        }

        private int GetPassCountByDate(string date)
        {
            try
            {
                string sql = "SELECT COUNT(*) FROM TestResult WHERE  TestResult.TestTime  >= #" + date + " 00:00:00# AND  TestResult.TestTime  <= #" + date + " 23:59:59# AND TestResult.Result = 1";

                Access acc = new Access();
                acc.Open();
                DataTable dt = acc.SelectToDataTable(sql);
                acc.Close();
                if (dt != null && dt.Rows.Count > 0)
                {
                    return int.Parse(dt.Rows[0][0].ToString());
                }

            }
            catch (Exception e)
            {

                //MessageBox.Show(e.Message);
            }
            return 0;
        }

        private int GetErrorCountByDate(string date)
        {
            try
            {
                string sql = "SELECT COUNT(*) FROM TestResult WHERE  TestResult.TestTime  >= #" + date + " 00:00:00# AND  TestResult.TestTime  <= #" + date + " 23:59:59# AND TestResult.Result = 0";

                Access acc = new Access();
                acc.Open();
                DataTable dt = acc.SelectToDataTable(sql);
                acc.Close();
                if (dt != null && dt.Rows.Count > 0)
                {
                    return int.Parse(dt.Rows[0][0].ToString());
                }
            }
            catch (Exception e)
            {

                // MessageBox.Show(e.Message);
            }
            return 0;
        }

        /// <summary>
        /// 日期格式设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

            int pass = GetPassCountByDate(this.dateTimePicker1.Value.ToString("yyyy-MM-dd"));
            int error = GetErrorCountByDate(this.dateTimePicker1.Value.ToString("yyyy-MM-dd"));

            labAll.Invoke(new EventHandler(delegate
            {
                labAll.Text = (pass + error).ToString();
            }));

            labPass.Invoke(new EventHandler(delegate
            {
                labPass.Text = pass.ToString();
            }));
            labError.Invoke(new EventHandler(delegate
            {
                labError.Text = error.ToString();
            }));
        }


        /// <summary>
        /// 双击label2，打开设置界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Middle)
            {
                PortSetting fs = new PortSetting();
                fs.ShowDialog();
                this.Refresh();
            }
        }


        /// <summary>
        ///   双击label4，打开设置界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label4_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Middle)
            {
                FormSetting fs = new FormSetting();
                fs.ShowDialog();
                this.Refresh();
            }
        }

        private void label6_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }
    }
}
